package booking.misc;

import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class Airport {
    
	private int airportId;
    private String airportName;
    private String airportCity;
    private String airportCountry;
    
    public Airport(int id, String name, String city, String country) {
    	airportId		= id;
    	airportName 	= name;
    	airportCity 	= city;
    	airportCountry  = country;
    }

	public Airport(ActionListener actionListener) {
		// TODO Auto-generated constructor stub
	}

	public int getAirportId() {
		return airportId;
	}

	public void setAirportId(int airportId) {
		this.airportId = airportId;
	}

	public String getAirportName() {
		return airportName;
	}

	public void setAirportName(String airportName) {
		this.airportName = airportName;
	}

	public String getAirportCity() {
		return airportCity;
	}

	public void setAirportCity(String airportCity) {
		this.airportCity = airportCity;
	}

	public String getAirportCountry() {
		return airportCountry;
	}

	public void setAirportCountry(String airportCountry) {
		this.airportCountry = airportCountry;
	}

	public String toString() {
		return getAirportName() + "," + getAirportCity();
	}

	public boolean dbInsert() {
        String sql = "INSERT INTO airports (NAME, City, Country) "
                + "VALUES (?, ?, ?)";
        try (Connection conn = DBConnectionData.getConnection()) {
            PreparedStatement statement = conn.prepareStatement(sql,
                    PreparedStatement.RETURN_GENERATED_KEYS);
            statement.setString(1, airportName);
            statement.setString(2, airportCity);
            statement.setString(3, airportCountry);
            statement.executeUpdate();
            ResultSet keys = statement.getGeneratedKeys();
            if (keys.next()) {
                airportId = keys.getInt(1);
            }
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }
    public boolean dbUpdate() {
        String sql = "UPDATE airports SET NAME=?, City=?, Country=? "
                + "WHERE ID=?";
        try (Connection conn = DBConnectionData.getConnection()) {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, airportName);
            statement.setString(2, airportCity);
            statement.setString(3, airportCountry);
            statement.setInt(4, airportId);
            statement.executeUpdate();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }
    public boolean dbDelete() {
        String sql = "DELETE FROM airports WHERE ID=?";
        try (Connection conn = DBConnectionData.getConnection()) {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setInt(1, airportId);
            statement.executeUpdate();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }
    public static Vector<Airport> loadAirports() {
        Vector<Airport> vec = new Vector<Airport>();
        try (Connection conn = DBConnectionData.getConnection()) {
            String sql = "SELECT ID, NAME, City, Country "
                    + "FROM airports ORDER BY Country ASC";
            Statement statement = conn.createStatement();
            ResultSet result = statement.executeQuery(sql);
            while (result.next()) {
            	Airport airport = new Airport(result.getInt(1), result.getString(2),
                        result.getString(3), result.getString(4));
                vec.add(airport);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return vec;
    }
}

